<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :modal-append-to-body="false"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="70%"
    >
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        ref="crud"
        v-model="form"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
        @row-click="rowClick"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            plain
            icon="el-icon-check"
            v-show="isMultiple"
            @click="btnchoice"
            >使用选中值
          </el-button>
          <el-button
            type="primary"
            plain
            icon="el-icon-close"
            @click="btncancel"
            >取消
          </el-button>
        </template>
      </avue-crud>
    </el-dialog>
  </div>
</template>
<script>
// import { getPage } from ''

export default {
  props: {
    isMultiple: {
      type: Boolean, //是否多选
      default: () => {
        return false;
      },
    },
  },
  data() {
    return {
      title: "选择用户",
      dialogVisible: false,
      form: {},
      query: {},
      loading: true,
      fullscreen: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      data: [],
      option: {
        indexLabel: "序号",
        calcHeight: 30,
        dialogWidth: 950,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: false,
        index: true,
        labelWidth: 120,
        searchLabelWidth: 90,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        selection: this.isMultiple,
        excelBtn: false,
        dialogClickModal: false,
        card: true,
        column: [
          {
            prop: "username",
            label: "用户名称",
            search: true,
          },
          {
            prop: "nickName",
            label: "用户昵称",
          },
          {
            prop: "email",
            label: "邮箱",
          },
          {
            prop: "phonenumber",
            label: "手机",
            search: true,
          },
          {
            type: "radio",
            label: "状态",
            dicData: [
              {
                label: "正常",
                value: "1",
              },
              {
                label: "停用",
                value: "0",
              },
            ],
            span: 12,
            display: true,
            props: {
              label: "label",
              value: "value",
              desc: "desc",
            },
            prop: "status",
            value: "1",
          },
          {
            prop: "createTime",
            label: "创建时间",
          },
        ],
      },
    };
  },

  methods: {
    init() {
      this.dialogVisible = true;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let values = {
        ...params,
        descs: "create_time",
      };
      this.loading = true;
      getPage(
        page.currentPage,
        page.pageSize,
        Object.assign(values, this.query)
      ).then((res) => {
        const data = res.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    rowClick(item) {
      if (this.isMultiple) {
        return;
      }
      let aa = item;
      let arr = [];
      arr.push(aa);
      this.$emit("callback_fuc_choice_xianlu", arr);
      this.btncancel();
    },
    btnchoice() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$emit("callback_fuc_choice_xianlu", this.selectionList);
      this.btncancel();
    },
    btncancel() {
      this.dialogVisible = false;
    },
  },
};
</script>
